package github.kawaiior.yuanchuxiuzhen.common.gongfa.base;

import com.sun.javafx.geom.Vec3d;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.GongFaBase;
import github.kawaiior.yuanchuxiuzhen.core.init.GongFaInit;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import github.kawaiior.yuanchuxiuzhen.util.EntityUtil;
import net.minecraft.block.Block;
import net.minecraft.block.IGrowable;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.common.util.LazyOptional;

public class BaiCaoZhenJing extends GongFaBase {
    public BaiCaoZhenJing(String itemName) {
        super(ManaPower.ManaRoot.WOOD, 3000, GongFaInit.QING_MU_JUE, itemName);
    }


    @Override
    public void onPlayerLearn(PlayerEntity player, World world, ManaPower power) {

    }

    @Override
    public void onPlayerLearnFinish(PlayerEntity player, World world, ManaPower power) {
        // TODO: 添加效果

    }

    public static void onPlayerDestroyBlock(PlayerEntity player, World world, Block block, ManaPower power){
        learn(player, world, block, power);
    }

    private static void learn(PlayerEntity player, World world, Block block, ManaPower power){
        if (!power.isLearningGongFa(GongFaInit.BAI_CAO_ZHEN_JING)){
            return;
        }

        int experience;
        if (block instanceof IGrowable || block.getDefaultState().getMaterial()== Material.WOOD){
            experience = 1;
        }else {
            return;
        }

        power.addGongFaExp(player, world, experience);
    }

    // 以下为效果
    public static void onPlayerLeftClick(PlayerEntity player, World world, ManaPower power){
        if (!power.isLearnedGongFa(GongFaInit.BAI_CAO_ZHEN_JING)){
            return;
        }

        if (player.getHeldItemMainhand().isEmpty() && power.consumeMana(player, world, 1000)){
            Entity target = EntityUtil.getPlayerLookTarget(world, player, 128D);

            LightningBoltEntity lightningBolt = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, world);
            lightningBolt.setDamage(power.getAttack() * 2 + 10);

            if (target!=null){
                lightningBolt.setPosition(target.getPosX(), target.getPosY(), target.getPosZ());
            }else {
                BlockPos pos = EntityUtil.getPlayerLookBlockPos(player, 128D);
                lightningBolt.setPosition(pos.getX(), pos.getY(), pos.getZ());
            }

            player.addPotionEffect(new EffectInstance(Effects.SPEED, 20*30, 1));
            world.addEntity(lightningBolt);
        }
    }
}
